package com.tip.calculator.view;

import com.tip.calculator.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class Configuration extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	public static final String MINIMUM_KEY = "min";
	public static final String MAXIMUM_KEY = "max";
	public static final String CHECKBOX_KEY = "checkBox";
	public static final String DEFAULT_MIN = "0";
	public static final String DEFAULT_MAX = "25";

	private CheckBoxPreference checkBoxPrefs;
	private EditTextPreference minPrefs;
	private EditTextPreference maxPrefs;
	private SharedPreferences sharedPreferences;
	private float minValue;
	private float maxValue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.prefs);

		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		checkBoxPrefs = (CheckBoxPreference) getPreferenceScreen()
				.findPreference(CHECKBOX_KEY);
		minPrefs = (EditTextPreference) getPreferenceScreen().findPreference(
				MINIMUM_KEY);
		maxPrefs = (EditTextPreference) getPreferenceScreen().findPreference(
				MAXIMUM_KEY);
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		// Setup the initial values
		minPrefs.setSummary("Current minimum is "
				+ sharedPreferences.getString(MINIMUM_KEY, ""));
		maxPrefs.setSummary("Current maximum is "
				+ sharedPreferences.getString(MAXIMUM_KEY, ""));

		// Set up a listener whenever a key changes
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {

		if (key.equals(CHECKBOX_KEY)) {

			resetPercentagesToDefault();
		}

		if (key.equals(MINIMUM_KEY)) {
			minValue = Float.parseFloat(sharedPreferences.getString(
					MINIMUM_KEY, "0"));
			maxValue = Float.parseFloat(sharedPreferences.getString(
					MAXIMUM_KEY, "0"));

			if (maxValue < minValue) {
				showPopUp("You cannot set the minimum greater than the maximum. Values are reset to default.");
				resetPercentagesToDefault();
			} else if (minValue == maxValue) {
				showPopUp("You cannot set the minimum the same as the maximum. Values are reset to default.");
				resetPercentagesToDefault();
			} else {
				minPrefs.setSummary("Current maximum is "
						+ sharedPreferences.getString(MINIMUM_KEY, ""));
			}
		}

		if (key.equals(MAXIMUM_KEY)) {
			minValue = Float.parseFloat(sharedPreferences.getString(
					MINIMUM_KEY, "0"));
			maxValue = Float.parseFloat(sharedPreferences.getString(
					MAXIMUM_KEY, "0"));

			if (maxValue < minValue) {
				showPopUp("You cannot set the maximum less than the minimum. Values are reset to default.");
				resetPercentagesToDefault();
			} else if (minValue == maxValue) {
				showPopUp("You cannot set the maximum the same as the minimum. Values are reset to default.");
				resetPercentagesToDefault();
			} else if (maxValue > 100) {
				showPopUp("You cannot set the maximum more than 100. Values are reset to default.");
				resetPercentagesToDefault();
			} else {
				maxPrefs.setSummary("Current maximum is "
						+ sharedPreferences.getString(MAXIMUM_KEY, ""));
			}
		}

	}

	public void resetPercentagesToDefault() {
		minPrefs.setText(DEFAULT_MIN);
		maxPrefs.setText(DEFAULT_MAX);
		minPrefs.setSummary("Current Minimum is " + DEFAULT_MIN);
		maxPrefs.setSummary("Current Maximum is " + DEFAULT_MAX);
	}

	public void showPopUp(String error) {
		AlertDialog alert = new AlertDialog.Builder(this)
				.setTitle("Percentage Error!")
				.setMessage(error)
				.setNeutralButton("Okay",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								Log.d("AlertDialog", "Neutral");
							}
						}).show();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}


}
